/**
 * MoveKnot.cpp
 * Coyright (c) 2009 Xiaofeng Gu. All Rights Reserved.
 * Author : Xiaofeng Gu
 *
 * Creation Data : 2009/07/23
 */

#include "stdafx.h"
#include "MoveKnot.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

HRESULT CMoveKnot::OnExecute()
{
	HRESULT hr = S_OK;
	hr = __super::OnExecute();
	if (hr != S_OK)
	{
		return hr;
	}

	CSKInfo* pSKInfo = GetBlockInfo<CSKInfo>(SKINFO_INSTANCE);
	CTimeline* pTimeline = pSKInfo->GetTimeline();
	CFamily* pFamily = pTimeline->GetActiveFamily();
	m_pKnot = pFamily->GetActiveChild();
	CSKObjectMoveObject& oMove = pSKInfo->GetMoveDefinition();
	Move(oMove);

	return S_OK;
}

void CMoveKnot::Move(const CSKObjectMoveObject& move)
{
	if (move.bActiveNode)
	{
		Move(move.dDistance);
	}
}

void CMoveKnot::Move(double distance)
{
	// get active node
	CTransNode* pHighLightNode = GetHighLightNode();
	if (pHighLightNode == NULL) return;

	CVec Direction = pHighLightNode->GetBone()->Direction();
	CTransNode* pTopNode = m_pKnot->GetTopNode();
	Direction.Identify();

	Direction *= distance;

	pTopNode->Move(Direction);
}


CTransNode* CMoveKnot::GetHighLightNode()
{
	Nodes* pNodes = m_pKnot->GetNodes();
	for (Nodes::iterator it = pNodes->begin();
		it != pNodes->end(); it ++)
	{
		CTransNode* pNode = *it;
		if (pNode->IsHighLight())
		{
			return pNode;
		}
	}

	return NULL;
}